// pages/myRank/myRank.js
const RANKS = [
  { name: '新兵出征', points: 0, icon: 'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/mine/rankImg/1.png' },
  { name: '勇士前行', points: 100, icon: 'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/mine/rankImg/2.png' },
  { name: '战将无畏', points: 200, icon: 'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/mine/rankImg/3.png' },
  { name: '元帅谋略', points: 500, icon: 'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/mine/rankImg/4.png' },
  { name: '英雄传奇', points: 1000, icon: 'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/mine/rankImg/5.png' }
];

Page({
  data: {
    points: 0,
    rankList: [],
    currentRank: null,
    progress: 0,
    nextRankNeed: 0
  },

  onLoad(options) {
    const points = parseInt(options.points) || 0;
    this.initRankData(points);
  },

  initRankData(points) {
    // 获取当前段位
    const currentRank = this.getCurrentRank(points);
    
    // 处理段位列表数据
    const rankList = RANKS.map(rank => ({
      ...rank,
      unlocked: points >= rank.points
    }));

    // 计算进度
    const nextRank = RANKS.find(r => r.points > currentRank.points);
    const nextRankNeed = nextRank ? nextRank.points - points : 0;
    const maxPoints = nextRank ? nextRank.points : currentRank.points;
    const progress = ((points - currentRank.points) / (maxPoints - currentRank.points)) * 100 || 0;

    this.setData({
      points,
      rankList,
      currentRank,
      progress: progress > 100 ? 100 : progress,
      nextRankNeed: nextRankNeed || 0
    });
  },

  getCurrentRank(points) {
    let current = RANKS[0];
    for (const rank of RANKS) {
      if (points >= rank.points) {
        current = rank;
      } else {
        break;
      }
    }
    return current;
  }
});